package cn.edu.ncu.housinghub.controller.test;

import cn.edu.ncu.housinghub.util.OssUtil;
import cn.edu.ncu.housinghub.vo.Result;
import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * OSS测试控制器
 */
@RestController
@RequestMapping("/test/oss")
@CrossOrigin(origins = "*")
public class OssTestController {

    @Autowired
    private OSS ossClient;

    @Autowired
    private OssUtil ossUtil;

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    /**
     * 测试OSS连接
     */
    @GetMapping("/connection")
    public Result testConnection() {
        try {
            // 测试bucket是否存在
            boolean exists = ossClient.doesBucketExist(bucketName);
            
            if (exists) {
                return Result.success("OSS连接成功，Bucket存在: " + bucketName);
            } else {
                return Result.error(500, "Bucket不存在: " + bucketName);
            }
        } catch (Exception e) {
            return Result.error(500, "OSS连接失败: " + e.getMessage());
        }
    }

    /**
     * 获取OSS配置信息
     */
    @GetMapping("/config")
    public Result getConfig() {
        try {
            return Result.success("OSS配置信息 - Endpoint: " + endpoint + ", Bucket: " + bucketName);
        } catch (Exception e) {
            return Result.error(500, "获取配置失败: " + e.getMessage());
        }
    }
}
